//
//  Person.m
//  ProjectDemo
//
//  Created by IOS3 on 2018/11/9.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import "Person.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "Car.h"

@implementation Person


void runImpl(id self, SEL _cmd) {
    NSLog(@"run");
}

//+ (BOOL)resolveInstanceMethod:(SEL)sel {
//    if (sel == @selector(run)) {
//        class_addMethod(self, sel, (IMP)runImpl, "v@:");
//        return YES;
//    }
//    return [super resolveInstanceMethod:sel];
//}


//- (id)forwardingTargetForSelector:(SEL)aSelector {
//    return Car.new;
//}


- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {

    NSString *sel = NSStringFromSelector(aSelector);
    if([sel isEqualToString:@"run"]) {
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }

    NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];

    return signature;
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    SEL selector = [anInvocation selector];
    Car *car = Car.new;
    if ([car respondsToSelector:selector]) {
        [anInvocation invokeWithTarget:car];
    }
}
@end
